<?php 
namespace app\index\controller;

use think\Controller;
use app\index\model\User as UserModel;
use think\captcha\Captcha;
use think\Session;

//比较正确,发送邮件模块

header("Content-type: text/html; charset=utf-8"); 
//验证类

//登录页面展示
class Login extends Controller{

	public function index(){

		// echo "登录页面";
		return $this->fetch();
	}

	//接收登录页面表单信息
	public function indexForm(){

		$LoginInfo = input('post.');
		/*dump($LoginInfo);

		echo md5($LoginInfo['password']);*/

        if(request()->isPost()){
            $data = input('post.');
            if(!captcha_check($data['verifyCode'])) {
                // 校验失败
                $this->error('验证码不正确');
            }
        }

		//给出模型
		$userModel = new UserModel();
		$userResult = $userModel->where('username',$LoginInfo['username'])->find();


		//没有数据证明账号不存在
		if(!$userResult){

             $this->error("账号不存在") ;
		}

        //转为数组
        $data = $userResult->getData();
		//dump($data);die;
		//比较密码
        //echo md5($LoginInfo['password']);die;
		if($data['password'] == md5($LoginInfo['password'])){


			//比较正确,设置SESSION 跳index
			Session::set('userid',$data['id']);
			Session::set('username',$data['username']);
			Session::set('userstatus',$data['status']);

			 $this->redirect('Come/index');
		}else{
			//错误,跳error
			 $this->error("密码错误");
			//exit;
		}

	}

	//跳转注册页面
	public function redirectRegister(){
		$this->redirect("Register/index");
	}

	//忘记密码页面展示
	public function forgotpass(){
		//echo 11;die;
		return $this->fetch();
	}

	public function forgotpassForm(){

		$ForgotPassEmail = input('post.');
		//dump($ForgotPassEmail);

		//得到账号跟email
        //给出模型
        $userModel = new UserModel();
        $userResult = $userModel->where('username',$ForgotPassEmail['username'])->find();
        //转为数组
        $data = $userResult->getData();
        //dump($data);die;
        //比较邮箱
        if($data['email'] == $ForgotPassEmail['email']){
            //比较正确,发送邮件模块
            //echo "邮箱正确";
            //exit;
        }else{
            //错误,跳error
            //echo "邮箱错误";
            //exit;
        }

		//发送消息给email
	}

	//锁定屏幕

	public function lock(){

		return $this->fetch();
	}

	//接受锁定屏幕表单

	public function lockForm(){

        $LoginInfo = input('post.');
        //dump($LoginInfo);


        //给出模型
        $userModel = new UserModel();
        $userResult = $userModel->where('username',$LoginInfo['username'])->find();
        //转为数组
        $data = $userResult->getData();
        //dump($data);die;
        //比较密码
        if($data['password'] == md5($LoginInfo['password'])){
            //比较正确,跳index
            //echo "密码正确";

             $this->redirect('Come/index');
           // return $this->redirect('Come/index',array('type'=>$type));
        }else{
            //错误,跳error
            //echo "密码错误";
             $this->error("密码错误");
        }

	}

	//登出,销毁SESSION
 public function logout(){
 	session_start();
 	Session::destroy();
 	$this->redirect('Login/index');
 }

}
 ?>
